package top.meeting.room.common.factory;


import com.jfinal.render.ErrorRender;
import com.jfinal.render.JsonRender;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/23
 * FOR : 重写RenderFactory
 */
public class JKhaledErrorRender extends ErrorRender {

    private static String JSON_400 = "{\"code\":400,\"msg\":\"参数错误\"}";
    private static String JSON_401 = "{\"code\":401,\"msg\":\"权限不足\"}";
    private static String JSON_404 = "{\"code\":404,\"msg\":\"找不到对应请求\"}";
    private static String JSON_405 = "{\"code\":405,\"msg\":\"请求的方法不对\"}";
    private static String JSON_500 = "{\"code\":500,\"msg\":\"服务器错误\"}";

    private JsonRender jsonRender;                    // 错误使用json传输出去

    /**
     * 构造器
     *
     * @param errorCode
     * @param view
     */
    public JKhaledErrorRender(int errorCode, String view) {
        super(errorCode, view);
    }

    /**
     * 重写页面输出方式
     */
    @Override
    public void render() {
        response.setStatus(getErrorCode());            // HttpServletResponse.SC_XXX_XXX

        switch (getErrorCode()) {
            case 400: {
                jsonRender = new JsonRender(JSON_400);
                break;
            }
            case 404: {
                jsonRender = new JsonRender(JSON_404);
                break;
            }
            case 401: {
                jsonRender = new JsonRender(JSON_401);
                break;
            }
            case 500: {
                jsonRender = new JsonRender(JSON_500);
                break;
            }
            case 405: {
                jsonRender = new JsonRender(JSON_405);
                break;
            }
            default: {
                super.render();
                return;
            }
        }
        jsonRender.setContext(request, response);    //传递给jsonRender 不然没法输出
        jsonRender.render();
    }

}
